package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.Page;
import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysUser;
import com.lyf.emall.mapper.RoleMapper;
import com.lyf.emall.mapper.UserMapper;
import com.lyf.emall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/2/17-15:13
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Transactional
    @Override
    public ResponseResult addUser(SysUser user) {
        int insertSysUser = userMapper.insertSysUser(user);
        int insertRoleId = roleMapper.insertRoleId(user.getUserId(), 3);
        if(insertSysUser == 0 && insertRoleId == 0){
            return new ResponseResult(500, "添加用户失败");
        }
        return new ResponseResult(200, "添加用户成功");
    }

    @Override
    public ResponseResult isSameUserName(String userName) {
        if(userName == null || userName.length() == 0){
            return new ResponseResult(400, "用户名不能为空");
        }
        SysUser user = userMapper.querySysUserByUserName(userName);
        // 用户为空则用户不存在
        if(Objects.isNull(user)){
            return new ResponseResult(400, "用户名不重复");
        }else{
            return new ResponseResult(200, "用户名重复");
        }
    }

    @Override
    public ResponseResult deleteUser(Integer userId) {
        if (userId == null || userId < 0){
            return new ResponseResult(400, "请求参数非法");
        }
        int updateDelFlagByUserId = userMapper.updateDelFlagByUserId(userId);
        if (updateDelFlagByUserId == 0){
            return new ResponseResult(400, "用户删除失败");
        }
        return new ResponseResult(200, "用户已经删除");
    }

    @Override
    public ResponseResult stopUser(Integer userId, Integer status) {
        if (userId == null || userId < 0){
            return new ResponseResult(400, "请求参数非法");
        }
        int updateStatusByUserId = userMapper.updateStatusByUserId(userId, status);
        if (updateStatusByUserId == 0){
            return new ResponseResult(400, "用户修改状态失败");
        }
        return new ResponseResult(200, "用户修改用户成功");
    }

    @Override
    public ResponseResult getUserInfo(Integer userId) {
        if (userId == null || userId < 0){
            return new ResponseResult(200, "请求参数非法");
        }
        SysUser user = userMapper.querySysUserByUserId(userId);
        if (user == null){
            return new ResponseResult(200, "没有该用户的信息");
        }
        Map<String,SysUser> map = new HashMap<>();
        map.put("user", user);
        return new ResponseResult(200, "获得用户信息成功", map);
    }

    @Override
    public ResponseResult updateUserInfo(SysUser user) {
        int updateSysUserByUserId = userMapper.updateSysUserByUserId(user);
        if (updateSysUserByUserId == 0){
            return new ResponseResult(400, "修改用户信息失败");
        }
        return new ResponseResult(200, "修改成功");
    }

    @Override
    public ResponseResult updatePassword(SysUser user) {
        if (user == null){
            return new ResponseResult(400, "数据不能为空");
        }
        int updatePasswordByUserId = userMapper.updatePasswordByUserId(user);
        if (updatePasswordByUserId == 0){
            return new ResponseResult(400, "修改密码失败");
        }
        return new ResponseResult(200, "修改密码成功");
    }

    @Override
    public ResponseResult getAllUserByPage(Page page) {
        if (page.getPageNo() < 0){
            new ResponseResult(200, "页码不能为负数");
        }
        // 设置pageHelper 页码+每页的数量
        PageHelper.startPage(page.getPageNo(),page.getPageNum());
        // 查询所有数据
        List<SysUser> users = userMapper.queryAllSysUser();
        // 获得分页的数据
        PageInfo<SysUser> pageInfo = new PageInfo<>(users);

        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获得用户列表成功", map);
    }

    @Override
    public ResponseResult getSysUserByUserName(String userName, Page page) {
        if (userName == null || userName.length() == 0 || " ".equals(userName)){
            return new ResponseResult(400, "用户名不能为空");
        }
        // 设置pageHelper 页码+每页的数量
        PageHelper.startPage(page.getPageNo(),page.getPageNum());
        List<SysUser> users = userMapper.queryAllSysUserByUserName(userName);
        PageInfo<SysUser> pageInfo = new PageInfo<>(users);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "查询用户成功", map);
    }

    @Override
    public ResponseResult getAllDeleteUser(Page page) {
        if (page.getPageNo() < 0){
            new ResponseResult(400, "页码不能为负数");
        }
        // 设置pageHelper 页码+每页的数量
        PageHelper.startPage(page.getPageNo(),page.getPageNum());
        List<SysUser> sysUsers = userMapper.queryAllDeleteSysUser();
        // 获得分页的数据
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获得用户列表成功", map);
    }

    @Override
    public ResponseResult restore(Integer userId) {
        int updateDelFlagByUserId = userMapper.updateSysUserDelFlag(userId);
        if(updateDelFlagByUserId < 0){
            return new ResponseResult(400, "还原失败");
        }
        return new ResponseResult(200, "还原成功");
    }

    @Override
    public ResponseResult getDeleteUserByUserName(String userName, Page page) {
        if (userName == null || userName.length() == 0 || " ".equals(userName)){
            return new ResponseResult(400, "用户名不能为空");
        }
        // 设置pageHelper 页码+每页的数量
        PageHelper.startPage(page.getPageNo(),page.getPageNum());
        List<SysUser> users = userMapper.queryAllDeleteUserByUserName(userName);
        PageInfo<SysUser> pageInfo = new PageInfo<>(users);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "查询用户成功", map);
    }
}
